1 using System;
2 using System.Collections.Generic;
3 using UnityEditor;
4
5 namespace UnityStandardAssets.CrossPlatformInput.Inspector
6 {
7 [InitializeOnLoad]
8 public class CrossPlatformInitialize
9 {
10 // Custom compiler defines:
11 //
12 // CROSS_PLATFORM_INPUT : denotes that cross platform input package exists, so that other packages can use their CrossPlatformInput functions.
13 // EDITOR_MOBILE_INPUT : denotes that mobile input should be used in editor, if a mobile build target is selected. (i.e. using Unity Remote app).
14 // MOBILE_INPUT : denotes that mobile input should be used right now!
15
16 static CrossPlatformInitialize()
17 {
18 var defines = GetDefinesList(buildTargetGroups[0]);
19 if (!defines.Contains("CROSS_PLATFORM_INPUT"))
20 {
21 SetEnabled("CROSS_PLATFORM_INPUT", true, false);
22 SetEnabled("MOBILE_INPUT", true, true);
23 }
24 }
25
26
27 [MenuItem("Mobile Input/Enable")]
28 private static void Enable()
29 {
30 SetEnabled("MOBILE_INPUT", true, true);
31 switch (EditorUserBuildSettings.activeBuildTarget)
32 {
33 case BuildTarget.Android:
34 case BuildTarget.iOS:
35 case BuildTarget.WP8Player:
36 case BuildTarget.BlackBerry:
37 case BuildTarget.PSM:
38 case BuildTarget.Tizen:
39 case BuildTarget.WSAPlayer:
40 EditorUtility.DisplayDialog("Mobile Input",
41 "You have enabled Mobile Input. You'll need to use the Unity Remote app on a connected device to control your game in the Editor.",
42 "OK");
43 break;
44
45 default:
46 EditorUtility.DisplayDialog("Mobile Input",
47 "You have enabled Mobile Input, but you have a non-mobile build target selected in your build settings. The mobile control rigs won't be active or visible on-screen until you switch the build target to a mobile platform.",
48 "OK");
49 break;
50 }
51 }
52
53
54 [MenuItem("Mobile Input/Enable", true)]
55 private static bool EnableValidate()
56 {
57 var defines = GetDefinesList(mobileBuildTargetGroups[0]);
58 return !defines.Contains("MOBILE_INPUT");
59 }
60
61
62 [MenuItem("Mobile Input/Disable")]
63 private static void Disable()
64 {
65 SetEnabled("MOBILE_INPUT", false, true);
66 switch (EditorUserBuildSettings.activeBuildTarget)
67 {
68 case BuildTarget.Android:
69 case BuildTarget.iOS:
70 case BuildTarget.WP8Player:
71 case BuildTarget.BlackBerry:
72 EditorUtility.DisplayDialog("Mobile Input",
73 "You have disabled Mobile Input. Mobile control rigs won't be visible, and the Cross Platform Input functions will always return standalone controls.",
74 "OK");
75 break;
76 }
77 }
78
79
80 [MenuItem("Mobile Input/Disable", true)]
81 private static bool DisableValidate()
82 {
83 var defines = GetDefinesList(mobileBuildTargetGroups[0]);
84 return defines.Contains("MOBILE_INPUT");
85 }
86
87
88 private static BuildTargetGroup[] buildTargetGroups = new BuildTargetGroup[]
89 {
90 BuildTargetGroup.Standalone,
91 BuildTargetGroup.WebPlayer,
92 BuildTargetGroup.Android,
93 BuildTargetGroup.iOS,
94 BuildTargetGroup.WP8,
95 BuildTargetGroup.BlackBerry
96 };
97
98 private static BuildTargetGroup[] mobileBuildTargetGroups = new BuildTargetGroup[]
99 {
100 BuildTargetGroup.Android,
101 BuildTargetGroup.iOS,
102 BuildTargetGroup.WP8,
103 BuildTargetGroup.BlackBerry,
104 BuildTargetGroup.PSM,
105 BuildTargetGroup.Tizen,
106 BuildTargetGroup.WSA
107 };
108
109
110 private static void SetEnabled(string defineName, bool enable, bool mobile)
111 {
112 //Debug.Log("setting "+defineName+" to "+enable);
113 foreach (var group in mobile ? mobileBuildTargetGroups : buildTargetGroups)
114 {
115 var defines = GetDefinesList(group);
116 if (enable)
117 {
118 if (defines.Contains(defineName))
119 {
120 return;
121 }
122 defines.Add(defineName);
123 }
124 else
125 {
126 if (!defines.Contains(defineName))
127 {
128 return;
129 }
130 while (defines.Contains(defineName))
131 {
132 defines.Remove(defineName);
133 }
134 }
135 string definesString = string.Join(";", defines.ToArray());
136 PlayerSettings.SetScriptingDefineSymbolsForGroup(group, definesString);
137 }
138 }
139
140
141 private static List<string> GetDefinesList(BuildTargetGroup group)
142 {
143 return new List<string>(PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';'));
144 }
145 }
146 }